/**
 * HT1621 空调液晶屏驱动
 * Copyright (c) 2021 深圳市智辰科技有限公司
 * All rights reserved.
 */

#include <string.h>

#include "nonos.h"
#include "lcd_ac.h"


// HT1621 初始化参数
static const uint8_t lcd_ac_init_params[] = {
	HT1621_CMD_SYS_EN,     	// Enable System
	HT1621_CMD_LCD_ON,     	// LCD ON
	HT1621_CMD_BIAS2_DUTY4,	// 1/2 Bias, 1/4 Duty
	HT1621_CMD_IRQ_NORMAL, 	// Normal Mode
	//HT1621_CMD_RC_256K,    	// on-chip RC震荡
	//HT1621_CMD_IRQ_DIS,    	// Disable IRQ
	//HT1621_CMD_TIMER_DIS,  	// Disable Timer
	//HT1621_CMD_WDT_DIS,    	// Disable WDT
	//HT1621_CMD_TONE_OFF,   	// Tone OFF
	//HT1621_CMD_TONE_4K,    	// Tone Frequency 4kHZ
};

void lcd_ac_init(const driver_ht1621_t* dev) {	
	// 初始化 HT1621
	ht1621_cmd(dev, lcd_ac_init_params, sizeof(lcd_ac_init_params));
	// 清除显示
	lcd_ac_clear(dev);
	// 测试
	//lcd_ac_test(dev);
	// 显示默认值
	lcd_ac_set(dev, LCD_MODE_COOL, LCD_FAN_SPEED_MEDIUM, 32, 25);
}

void lcd_ac_switch(const driver_ht1621_t* dev, uint8_t state) {
}

void lcd_ac_clear(const driver_ht1621_t* dev) {
	uint8_t ram[32]; memset(ram, 0, 32);
	ht1621_write(dev, 0x00, ram, sizeof(ram));
}

void lcd_ac_write(const driver_ht1621_t* dev, lcd_ac_ram_t *ram) {
	ht1621_write(dev, 0x00, ram->bytes, sizeof(ram->bytes));
}

void lcd_ac_read(const driver_ht1621_t* dev, lcd_ac_ram_t *ram) {
}

void lcd_ac_ram_clear(lcd_ac_ram_t *ram) {
	memset(ram->bytes, 0, sizeof(ram->bytes));
}

void lcd_ac_ram_set_base(lcd_ac_ram_t *ram, uint8_t visible) {
	visible = (visible != 0) ? 1 : 0;
	ram->st.S1 = visible;
	ram->st.DP1 = visible;
}

void lcd_ac_ram_set_char1(lcd_ac_ram_t *ram, lcd_ac_8seg_char_t ch) {
	switch(ch) {
		case LCD_8SEG_CHAR_0:         ram->st.N1A = 1; ram->st.N1B = 1; ram->st.N1C = 1; ram->st.N1D = 1; ram->st.N1E = 1; ram->st.N1F = 1; ram->st.N1G = 0; break;
		case LCD_8SEG_CHAR_1:         ram->st.N1A = 0; ram->st.N1B = 1; ram->st.N1C = 1; ram->st.N1D = 0; ram->st.N1E = 0; ram->st.N1F = 0; ram->st.N1G = 0; break;
		case LCD_8SEG_CHAR_2:         ram->st.N1A = 1; ram->st.N1B = 1; ram->st.N1C = 0; ram->st.N1D = 1; ram->st.N1E = 1; ram->st.N1F = 0; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_3:         ram->st.N1A = 1; ram->st.N1B = 1; ram->st.N1C = 1; ram->st.N1D = 1; ram->st.N1E = 0; ram->st.N1F = 0; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_4:         ram->st.N1A = 0; ram->st.N1B = 1; ram->st.N1C = 1; ram->st.N1D = 0; ram->st.N1E = 0; ram->st.N1F = 1; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_5:         ram->st.N1A = 1; ram->st.N1B = 0; ram->st.N1C = 1; ram->st.N1D = 1; ram->st.N1E = 0; ram->st.N1F = 1; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_6:         ram->st.N1A = 1; ram->st.N1B = 0; ram->st.N1C = 1; ram->st.N1D = 1; ram->st.N1E = 1; ram->st.N1F = 1; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_7:         ram->st.N1A = 1; ram->st.N1B = 1; ram->st.N1C = 1; ram->st.N1D = 0; ram->st.N1E = 0; ram->st.N1F = 0; ram->st.N1G = 0; break;
		case LCD_8SEG_CHAR_8:         ram->st.N1A = 1; ram->st.N1B = 1; ram->st.N1C = 1; ram->st.N1D = 1; ram->st.N1E = 1; ram->st.N1F = 1; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_9:         ram->st.N1A = 1; ram->st.N1B = 1; ram->st.N1C = 1; ram->st.N1D = 1; ram->st.N1E = 0; ram->st.N1F = 1; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_A:         ram->st.N1A = 1; ram->st.N1B = 1; ram->st.N1C = 1; ram->st.N1D = 0; ram->st.N1E = 1; ram->st.N1F = 1; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_b:         ram->st.N1A = 0; ram->st.N1B = 0; ram->st.N1C = 1; ram->st.N1D = 1; ram->st.N1E = 1; ram->st.N1F = 1; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_C:         ram->st.N1A = 1; ram->st.N1B = 0; ram->st.N1C = 0; ram->st.N1D = 1; ram->st.N1E = 1; ram->st.N1F = 1; ram->st.N1G = 0; break;
		case LCD_8SEG_CHAR_c:         ram->st.N1A = 0; ram->st.N1B = 0; ram->st.N1C = 0; ram->st.N1D = 1; ram->st.N1E = 1; ram->st.N1F = 0; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_d:         ram->st.N1A = 0; ram->st.N1B = 1; ram->st.N1C = 1; ram->st.N1D = 1; ram->st.N1E = 1; ram->st.N1F = 0; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_E:         ram->st.N1A = 1; ram->st.N1B = 0; ram->st.N1C = 0; ram->st.N1D = 1; ram->st.N1E = 1; ram->st.N1F = 1; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_F:         ram->st.N1A = 1; ram->st.N1B = 0; ram->st.N1C = 0; ram->st.N1D = 0; ram->st.N1E = 1; ram->st.N1F = 1; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_G:         ram->st.N1A = 1; ram->st.N1B = 0; ram->st.N1C = 1; ram->st.N1D = 1; ram->st.N1E = 1; ram->st.N1F = 1; ram->st.N1G = 0; break;
		case LCD_8SEG_CHAR_H:         ram->st.N1A = 0; ram->st.N1B = 1; ram->st.N1C = 1; ram->st.N1D = 0; ram->st.N1E = 1; ram->st.N1F = 1; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_h:         ram->st.N1A = 0; ram->st.N1B = 0; ram->st.N1C = 1; ram->st.N1D = 0; ram->st.N1E = 1; ram->st.N1F = 1; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_J:         ram->st.N1A = 0; ram->st.N1B = 1; ram->st.N1C = 1; ram->st.N1D = 1; ram->st.N1E = 0; ram->st.N1F = 0; ram->st.N1G = 0; break;
		case LCD_8SEG_CHAR_L:         ram->st.N1A = 0; ram->st.N1B = 0; ram->st.N1C = 0; ram->st.N1D = 1; ram->st.N1E = 1; ram->st.N1F = 1; ram->st.N1G = 0; break;
		case LCD_8SEG_CHAR_n:         ram->st.N1A = 0; ram->st.N1B = 0; ram->st.N1C = 1; ram->st.N1D = 0; ram->st.N1E = 1; ram->st.N1F = 0; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_o:         ram->st.N1A = 0; ram->st.N1B = 0; ram->st.N1C = 1; ram->st.N1D = 1; ram->st.N1E = 1; ram->st.N1F = 0; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_p:         ram->st.N1A = 1; ram->st.N1B = 1; ram->st.N1C = 0; ram->st.N1D = 0; ram->st.N1E = 1; ram->st.N1F = 1; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_q:         ram->st.N1A = 1; ram->st.N1B = 1; ram->st.N1C = 1; ram->st.N1D = 0; ram->st.N1E = 0; ram->st.N1F = 1; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_r:         ram->st.N1A = 0; ram->st.N1B = 0; ram->st.N1C = 0; ram->st.N1D = 0; ram->st.N1E = 1; ram->st.N1F = 0; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_t:         ram->st.N1A = 0; ram->st.N1B = 0; ram->st.N1C = 0; ram->st.N1D = 1; ram->st.N1E = 1; ram->st.N1F = 1; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_U:         ram->st.N1A = 0; ram->st.N1B = 1; ram->st.N1C = 1; ram->st.N1D = 1; ram->st.N1E = 1; ram->st.N1F = 1; ram->st.N1G = 0; break;
		case LCD_8SEG_CHAR_u:         ram->st.N1A = 0; ram->st.N1B = 0; ram->st.N1C = 1; ram->st.N1D = 1; ram->st.N1E = 1; ram->st.N1F = 0; ram->st.N1G = 0; break;
		case LCD_8SEG_CHAR_y:         ram->st.N1A = 0; ram->st.N1B = 1; ram->st.N1C = 1; ram->st.N1D = 1; ram->st.N1E = 0; ram->st.N1F = 1; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_MINUS:     ram->st.N1A = 0; ram->st.N1B = 0; ram->st.N1C = 0; ram->st.N1D = 0; ram->st.N1E = 0; ram->st.N1F = 0; ram->st.N1G = 1; break;
		case LCD_8SEG_CHAR_UNDERLINE: ram->st.N1A = 0; ram->st.N1B = 0; ram->st.N1C = 0; ram->st.N1D = 1; ram->st.N1E = 0; ram->st.N1F = 0; ram->st.N1G = 0; break;
		default: break;
	}		
}

void lcd_ac_ram_set_char2(lcd_ac_ram_t *ram, lcd_ac_8seg_char_t ch) {
	switch(ch) {
		case LCD_8SEG_CHAR_0:         ram->st.N2A = 1; ram->st.N2B = 1; ram->st.N2C = 1; ram->st.N2D = 1; ram->st.N2E = 1; ram->st.N2F = 1; ram->st.N2G = 0; break;
		case LCD_8SEG_CHAR_1:         ram->st.N2A = 0; ram->st.N2B = 1; ram->st.N2C = 1; ram->st.N2D = 0; ram->st.N2E = 0; ram->st.N2F = 0; ram->st.N2G = 0; break;
		case LCD_8SEG_CHAR_2:         ram->st.N2A = 1; ram->st.N2B = 1; ram->st.N2C = 0; ram->st.N2D = 1; ram->st.N2E = 1; ram->st.N2F = 0; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_3:         ram->st.N2A = 1; ram->st.N2B = 1; ram->st.N2C = 1; ram->st.N2D = 1; ram->st.N2E = 0; ram->st.N2F = 0; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_4:         ram->st.N2A = 0; ram->st.N2B = 1; ram->st.N2C = 1; ram->st.N2D = 0; ram->st.N2E = 0; ram->st.N2F = 1; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_5:         ram->st.N2A = 1; ram->st.N2B = 0; ram->st.N2C = 1; ram->st.N2D = 1; ram->st.N2E = 0; ram->st.N2F = 1; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_6:         ram->st.N2A = 1; ram->st.N2B = 0; ram->st.N2C = 1; ram->st.N2D = 1; ram->st.N2E = 1; ram->st.N2F = 1; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_7:         ram->st.N2A = 1; ram->st.N2B = 1; ram->st.N2C = 1; ram->st.N2D = 0; ram->st.N2E = 0; ram->st.N2F = 0; ram->st.N2G = 0; break;
		case LCD_8SEG_CHAR_8:         ram->st.N2A = 1; ram->st.N2B = 1; ram->st.N2C = 1; ram->st.N2D = 1; ram->st.N2E = 1; ram->st.N2F = 1; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_9:         ram->st.N2A = 1; ram->st.N2B = 1; ram->st.N2C = 1; ram->st.N2D = 1; ram->st.N2E = 0; ram->st.N2F = 1; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_A:         ram->st.N2A = 1; ram->st.N2B = 1; ram->st.N2C = 1; ram->st.N2D = 0; ram->st.N2E = 1; ram->st.N2F = 1; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_b:         ram->st.N2A = 0; ram->st.N2B = 0; ram->st.N2C = 1; ram->st.N2D = 1; ram->st.N2E = 1; ram->st.N2F = 1; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_C:         ram->st.N2A = 1; ram->st.N2B = 0; ram->st.N2C = 0; ram->st.N2D = 1; ram->st.N2E = 1; ram->st.N2F = 1; ram->st.N2G = 0; break;
		case LCD_8SEG_CHAR_c:         ram->st.N2A = 0; ram->st.N2B = 0; ram->st.N2C = 0; ram->st.N2D = 1; ram->st.N2E = 1; ram->st.N2F = 0; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_d:         ram->st.N2A = 0; ram->st.N2B = 1; ram->st.N2C = 1; ram->st.N2D = 1; ram->st.N2E = 1; ram->st.N2F = 0; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_E:         ram->st.N2A = 1; ram->st.N2B = 0; ram->st.N2C = 0; ram->st.N2D = 1; ram->st.N2E = 1; ram->st.N2F = 1; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_F:         ram->st.N2A = 1; ram->st.N2B = 0; ram->st.N2C = 0; ram->st.N2D = 0; ram->st.N2E = 1; ram->st.N2F = 1; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_G:         ram->st.N2A = 1; ram->st.N2B = 0; ram->st.N2C = 1; ram->st.N2D = 1; ram->st.N2E = 1; ram->st.N2F = 1; ram->st.N2G = 0; break;
		case LCD_8SEG_CHAR_H:         ram->st.N2A = 0; ram->st.N2B = 1; ram->st.N2C = 1; ram->st.N2D = 0; ram->st.N2E = 1; ram->st.N2F = 1; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_h:         ram->st.N2A = 0; ram->st.N2B = 0; ram->st.N2C = 1; ram->st.N2D = 0; ram->st.N2E = 1; ram->st.N2F = 1; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_J:         ram->st.N2A = 0; ram->st.N2B = 1; ram->st.N2C = 1; ram->st.N2D = 1; ram->st.N2E = 0; ram->st.N2F = 0; ram->st.N2G = 0; break;
		case LCD_8SEG_CHAR_L:         ram->st.N2A = 0; ram->st.N2B = 0; ram->st.N2C = 0; ram->st.N2D = 1; ram->st.N2E = 1; ram->st.N2F = 1; ram->st.N2G = 0; break;
		case LCD_8SEG_CHAR_n:         ram->st.N2A = 0; ram->st.N2B = 0; ram->st.N2C = 1; ram->st.N2D = 0; ram->st.N2E = 1; ram->st.N2F = 0; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_o:         ram->st.N2A = 0; ram->st.N2B = 0; ram->st.N2C = 1; ram->st.N2D = 1; ram->st.N2E = 1; ram->st.N2F = 0; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_p:         ram->st.N2A = 1; ram->st.N2B = 1; ram->st.N2C = 0; ram->st.N2D = 0; ram->st.N2E = 1; ram->st.N2F = 1; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_q:         ram->st.N2A = 1; ram->st.N2B = 1; ram->st.N2C = 1; ram->st.N2D = 0; ram->st.N2E = 0; ram->st.N2F = 1; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_r:         ram->st.N2A = 0; ram->st.N2B = 0; ram->st.N2C = 0; ram->st.N2D = 0; ram->st.N2E = 1; ram->st.N2F = 0; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_t:         ram->st.N2A = 0; ram->st.N2B = 0; ram->st.N2C = 0; ram->st.N2D = 1; ram->st.N2E = 1; ram->st.N2F = 1; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_U:         ram->st.N2A = 0; ram->st.N2B = 1; ram->st.N2C = 1; ram->st.N2D = 1; ram->st.N2E = 1; ram->st.N2F = 1; ram->st.N2G = 0; break;
		case LCD_8SEG_CHAR_u:         ram->st.N2A = 0; ram->st.N2B = 0; ram->st.N2C = 1; ram->st.N2D = 1; ram->st.N2E = 1; ram->st.N2F = 0; ram->st.N2G = 0; break;
		case LCD_8SEG_CHAR_y:         ram->st.N2A = 0; ram->st.N2B = 1; ram->st.N2C = 1; ram->st.N2D = 1; ram->st.N2E = 0; ram->st.N2F = 1; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_MINUS:     ram->st.N2A = 0; ram->st.N2B = 0; ram->st.N2C = 0; ram->st.N2D = 0; ram->st.N2E = 0; ram->st.N2F = 0; ram->st.N2G = 1; break;
		case LCD_8SEG_CHAR_UNDERLINE: ram->st.N2A = 0; ram->st.N2B = 0; ram->st.N2C = 0; ram->st.N2D = 1; ram->st.N2E = 0; ram->st.N2F = 0; ram->st.N2G = 0; break;
		default: break;
	}		
}

void lcd_ac_ram_set_char3(lcd_ac_ram_t *ram, lcd_ac_8seg_char_t ch) {
	switch(ch) {
		case LCD_8SEG_CHAR_0:         ram->st.N4A = 1; ram->st.N4B = 1; ram->st.N4C = 1; ram->st.N4D = 1; ram->st.N4E = 1; ram->st.N4F = 1; ram->st.N4G = 0; break;
		case LCD_8SEG_CHAR_1:         ram->st.N4A = 0; ram->st.N4B = 1; ram->st.N4C = 1; ram->st.N4D = 0; ram->st.N4E = 0; ram->st.N4F = 0; ram->st.N4G = 0; break;
		case LCD_8SEG_CHAR_2:         ram->st.N4A = 1; ram->st.N4B = 1; ram->st.N4C = 0; ram->st.N4D = 1; ram->st.N4E = 1; ram->st.N4F = 0; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_3:         ram->st.N4A = 1; ram->st.N4B = 1; ram->st.N4C = 1; ram->st.N4D = 1; ram->st.N4E = 0; ram->st.N4F = 0; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_4:         ram->st.N4A = 0; ram->st.N4B = 1; ram->st.N4C = 1; ram->st.N4D = 0; ram->st.N4E = 0; ram->st.N4F = 1; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_5:         ram->st.N4A = 1; ram->st.N4B = 0; ram->st.N4C = 1; ram->st.N4D = 1; ram->st.N4E = 0; ram->st.N4F = 1; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_6:         ram->st.N4A = 1; ram->st.N4B = 0; ram->st.N4C = 1; ram->st.N4D = 1; ram->st.N4E = 1; ram->st.N4F = 1; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_7:         ram->st.N4A = 1; ram->st.N4B = 1; ram->st.N4C = 1; ram->st.N4D = 0; ram->st.N4E = 0; ram->st.N4F = 0; ram->st.N4G = 0; break;
		case LCD_8SEG_CHAR_8:         ram->st.N4A = 1; ram->st.N4B = 1; ram->st.N4C = 1; ram->st.N4D = 1; ram->st.N4E = 1; ram->st.N4F = 1; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_9:         ram->st.N4A = 1; ram->st.N4B = 1; ram->st.N4C = 1; ram->st.N4D = 1; ram->st.N4E = 0; ram->st.N4F = 1; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_A:         ram->st.N4A = 1; ram->st.N4B = 1; ram->st.N4C = 1; ram->st.N4D = 0; ram->st.N4E = 1; ram->st.N4F = 1; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_b:         ram->st.N4A = 0; ram->st.N4B = 0; ram->st.N4C = 1; ram->st.N4D = 1; ram->st.N4E = 1; ram->st.N4F = 1; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_C:         ram->st.N4A = 1; ram->st.N4B = 0; ram->st.N4C = 0; ram->st.N4D = 1; ram->st.N4E = 1; ram->st.N4F = 1; ram->st.N4G = 0; break;
		case LCD_8SEG_CHAR_c:         ram->st.N4A = 0; ram->st.N4B = 0; ram->st.N4C = 0; ram->st.N4D = 1; ram->st.N4E = 1; ram->st.N4F = 0; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_d:         ram->st.N4A = 0; ram->st.N4B = 1; ram->st.N4C = 1; ram->st.N4D = 1; ram->st.N4E = 1; ram->st.N4F = 0; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_E:         ram->st.N4A = 1; ram->st.N4B = 0; ram->st.N4C = 0; ram->st.N4D = 1; ram->st.N4E = 1; ram->st.N4F = 1; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_F:         ram->st.N4A = 1; ram->st.N4B = 0; ram->st.N4C = 0; ram->st.N4D = 0; ram->st.N4E = 1; ram->st.N4F = 1; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_G:         ram->st.N4A = 1; ram->st.N4B = 0; ram->st.N4C = 1; ram->st.N4D = 1; ram->st.N4E = 1; ram->st.N4F = 1; ram->st.N4G = 0; break;
		case LCD_8SEG_CHAR_H:         ram->st.N4A = 0; ram->st.N4B = 1; ram->st.N4C = 1; ram->st.N4D = 0; ram->st.N4E = 1; ram->st.N4F = 1; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_h:         ram->st.N4A = 0; ram->st.N4B = 0; ram->st.N4C = 1; ram->st.N4D = 0; ram->st.N4E = 1; ram->st.N4F = 1; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_J:         ram->st.N4A = 0; ram->st.N4B = 1; ram->st.N4C = 1; ram->st.N4D = 1; ram->st.N4E = 0; ram->st.N4F = 0; ram->st.N4G = 0; break;
		case LCD_8SEG_CHAR_L:         ram->st.N4A = 0; ram->st.N4B = 0; ram->st.N4C = 0; ram->st.N4D = 1; ram->st.N4E = 1; ram->st.N4F = 1; ram->st.N4G = 0; break;
		case LCD_8SEG_CHAR_n:         ram->st.N4A = 0; ram->st.N4B = 0; ram->st.N4C = 1; ram->st.N4D = 0; ram->st.N4E = 1; ram->st.N4F = 0; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_o:         ram->st.N4A = 0; ram->st.N4B = 0; ram->st.N4C = 1; ram->st.N4D = 1; ram->st.N4E = 1; ram->st.N4F = 0; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_p:         ram->st.N4A = 1; ram->st.N4B = 1; ram->st.N4C = 0; ram->st.N4D = 0; ram->st.N4E = 1; ram->st.N4F = 1; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_q:         ram->st.N4A = 1; ram->st.N4B = 1; ram->st.N4C = 1; ram->st.N4D = 0; ram->st.N4E = 0; ram->st.N4F = 1; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_r:         ram->st.N4A = 0; ram->st.N4B = 0; ram->st.N4C = 0; ram->st.N4D = 0; ram->st.N4E = 1; ram->st.N4F = 0; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_t:         ram->st.N4A = 0; ram->st.N4B = 0; ram->st.N4C = 0; ram->st.N4D = 1; ram->st.N4E = 1; ram->st.N4F = 1; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_U:         ram->st.N4A = 0; ram->st.N4B = 1; ram->st.N4C = 1; ram->st.N4D = 1; ram->st.N4E = 1; ram->st.N4F = 1; ram->st.N4G = 0; break;
		case LCD_8SEG_CHAR_u:         ram->st.N4A = 0; ram->st.N4B = 0; ram->st.N4C = 1; ram->st.N4D = 1; ram->st.N4E = 1; ram->st.N4F = 0; ram->st.N4G = 0; break;
		case LCD_8SEG_CHAR_y:         ram->st.N4A = 0; ram->st.N4B = 1; ram->st.N4C = 1; ram->st.N4D = 1; ram->st.N4E = 0; ram->st.N4F = 1; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_MINUS:     ram->st.N4A = 0; ram->st.N4B = 0; ram->st.N4C = 0; ram->st.N4D = 0; ram->st.N4E = 0; ram->st.N4F = 0; ram->st.N4G = 1; break;
		case LCD_8SEG_CHAR_UNDERLINE: ram->st.N4A = 0; ram->st.N4B = 0; ram->st.N4C = 0; ram->st.N4D = 1; ram->st.N4E = 0; ram->st.N4F = 0; ram->st.N4G = 0; break;
		default: break;
	}		
}

void lcd_ac_ram_set_char4(lcd_ac_ram_t *ram, lcd_ac_8seg_char_t ch) {
	switch(ch) {
		case LCD_8SEG_CHAR_0:         ram->st.N5A = 1; ram->st.N5B = 1; ram->st.N5C = 1; ram->st.N5D = 1; ram->st.N5E = 1; ram->st.N5F = 1; ram->st.N5G = 0; break;
		case LCD_8SEG_CHAR_1:         ram->st.N5A = 0; ram->st.N5B = 1; ram->st.N5C = 1; ram->st.N5D = 0; ram->st.N5E = 0; ram->st.N5F = 0; ram->st.N5G = 0; break;
		case LCD_8SEG_CHAR_2:         ram->st.N5A = 1; ram->st.N5B = 1; ram->st.N5C = 0; ram->st.N5D = 1; ram->st.N5E = 1; ram->st.N5F = 0; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_3:         ram->st.N5A = 1; ram->st.N5B = 1; ram->st.N5C = 1; ram->st.N5D = 1; ram->st.N5E = 0; ram->st.N5F = 0; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_4:         ram->st.N5A = 0; ram->st.N5B = 1; ram->st.N5C = 1; ram->st.N5D = 0; ram->st.N5E = 0; ram->st.N5F = 1; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_5:         ram->st.N5A = 1; ram->st.N5B = 0; ram->st.N5C = 1; ram->st.N5D = 1; ram->st.N5E = 0; ram->st.N5F = 1; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_6:         ram->st.N5A = 1; ram->st.N5B = 0; ram->st.N5C = 1; ram->st.N5D = 1; ram->st.N5E = 1; ram->st.N5F = 1; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_7:         ram->st.N5A = 1; ram->st.N5B = 1; ram->st.N5C = 1; ram->st.N5D = 0; ram->st.N5E = 0; ram->st.N5F = 0; ram->st.N5G = 0; break;
		case LCD_8SEG_CHAR_8:         ram->st.N5A = 1; ram->st.N5B = 1; ram->st.N5C = 1; ram->st.N5D = 1; ram->st.N5E = 1; ram->st.N5F = 1; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_9:         ram->st.N5A = 1; ram->st.N5B = 1; ram->st.N5C = 1; ram->st.N5D = 1; ram->st.N5E = 0; ram->st.N5F = 1; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_A:         ram->st.N5A = 1; ram->st.N5B = 1; ram->st.N5C = 1; ram->st.N5D = 0; ram->st.N5E = 1; ram->st.N5F = 1; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_b:         ram->st.N5A = 0; ram->st.N5B = 0; ram->st.N5C = 1; ram->st.N5D = 1; ram->st.N5E = 1; ram->st.N5F = 1; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_C:         ram->st.N5A = 1; ram->st.N5B = 0; ram->st.N5C = 0; ram->st.N5D = 1; ram->st.N5E = 1; ram->st.N5F = 1; ram->st.N5G = 0; break;
		case LCD_8SEG_CHAR_c:         ram->st.N5A = 0; ram->st.N5B = 0; ram->st.N5C = 0; ram->st.N5D = 1; ram->st.N5E = 1; ram->st.N5F = 0; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_d:         ram->st.N5A = 0; ram->st.N5B = 1; ram->st.N5C = 1; ram->st.N5D = 1; ram->st.N5E = 1; ram->st.N5F = 0; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_E:         ram->st.N5A = 1; ram->st.N5B = 0; ram->st.N5C = 0; ram->st.N5D = 1; ram->st.N5E = 1; ram->st.N5F = 1; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_F:         ram->st.N5A = 1; ram->st.N5B = 0; ram->st.N5C = 0; ram->st.N5D = 0; ram->st.N5E = 1; ram->st.N5F = 1; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_G:         ram->st.N5A = 1; ram->st.N5B = 0; ram->st.N5C = 1; ram->st.N5D = 1; ram->st.N5E = 1; ram->st.N5F = 1; ram->st.N5G = 0; break;
		case LCD_8SEG_CHAR_H:         ram->st.N5A = 0; ram->st.N5B = 1; ram->st.N5C = 1; ram->st.N5D = 0; ram->st.N5E = 1; ram->st.N5F = 1; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_h:         ram->st.N5A = 0; ram->st.N5B = 0; ram->st.N5C = 1; ram->st.N5D = 0; ram->st.N5E = 1; ram->st.N5F = 1; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_J:         ram->st.N5A = 0; ram->st.N5B = 1; ram->st.N5C = 1; ram->st.N5D = 1; ram->st.N5E = 0; ram->st.N5F = 0; ram->st.N5G = 0; break;
		case LCD_8SEG_CHAR_L:         ram->st.N5A = 0; ram->st.N5B = 0; ram->st.N5C = 0; ram->st.N5D = 1; ram->st.N5E = 1; ram->st.N5F = 1; ram->st.N5G = 0; break;
		case LCD_8SEG_CHAR_n:         ram->st.N5A = 0; ram->st.N5B = 0; ram->st.N5C = 1; ram->st.N5D = 0; ram->st.N5E = 1; ram->st.N5F = 0; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_o:         ram->st.N5A = 0; ram->st.N5B = 0; ram->st.N5C = 1; ram->st.N5D = 1; ram->st.N5E = 1; ram->st.N5F = 0; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_p:         ram->st.N5A = 1; ram->st.N5B = 1; ram->st.N5C = 0; ram->st.N5D = 0; ram->st.N5E = 1; ram->st.N5F = 1; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_q:         ram->st.N5A = 1; ram->st.N5B = 1; ram->st.N5C = 1; ram->st.N5D = 0; ram->st.N5E = 0; ram->st.N5F = 1; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_r:         ram->st.N5A = 0; ram->st.N5B = 0; ram->st.N5C = 0; ram->st.N5D = 0; ram->st.N5E = 1; ram->st.N5F = 0; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_t:         ram->st.N5A = 0; ram->st.N5B = 0; ram->st.N5C = 0; ram->st.N5D = 1; ram->st.N5E = 1; ram->st.N5F = 1; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_U:         ram->st.N5A = 0; ram->st.N5B = 1; ram->st.N5C = 1; ram->st.N5D = 1; ram->st.N5E = 1; ram->st.N5F = 1; ram->st.N5G = 0; break;
		case LCD_8SEG_CHAR_u:         ram->st.N5A = 0; ram->st.N5B = 0; ram->st.N5C = 1; ram->st.N5D = 1; ram->st.N5E = 1; ram->st.N5F = 0; ram->st.N5G = 0; break;
		case LCD_8SEG_CHAR_y:         ram->st.N5A = 0; ram->st.N5B = 1; ram->st.N5C = 1; ram->st.N5D = 1; ram->st.N5E = 0; ram->st.N5F = 1; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_MINUS:     ram->st.N5A = 0; ram->st.N5B = 0; ram->st.N5C = 0; ram->st.N5D = 0; ram->st.N5E = 0; ram->st.N5F = 0; ram->st.N5G = 1; break;
		case LCD_8SEG_CHAR_UNDERLINE: ram->st.N5A = 0; ram->st.N5B = 0; ram->st.N5C = 0; ram->st.N5D = 1; ram->st.N5E = 0; ram->st.N5F = 0; ram->st.N5G = 0; break;
		default: break;
	}
}

void lcd_ac_ram_set_temp_room(lcd_ac_ram_t *ram, uint8_t val) {
	lcd_ac_8seg_char_t n1 = (lcd_ac_8seg_char_t)(val/10), n2 = (lcd_ac_8seg_char_t)(val % 10);
	lcd_ac_ram_set_char1(ram, n1);
	lcd_ac_ram_set_char2(ram, n2);
}

void lcd_ac_ram_set_temp_target(lcd_ac_ram_t *ram, uint8_t val) {
	lcd_ac_8seg_char_t n1 = (lcd_ac_8seg_char_t)(val/10), n2 = (lcd_ac_8seg_char_t)(val % 10);
	lcd_ac_ram_set_char3(ram, n1);
	lcd_ac_ram_set_char4(ram, n2);
}

void lcd_ac_ram_set_fan_speed(lcd_ac_ram_t *ram, lcd_ac_fan_speed_t speed) {	
	switch(speed) {
		case LCD_FAN_SPEED_LOW:    ram->st.F1 = 1; ram->st.F2 = 0; ram->st.F3 = 0; break;
		case LCD_FAN_SPEED_MEDIUM: ram->st.F1 = 0; ram->st.F2 = 1; ram->st.F3 = 0; break;
		case LCD_FAN_SPEED_HIGH:   ram->st.F1 = 0; ram->st.F2 = 0; ram->st.F3 = 1; break;
		default: break;
	}
}

void lcd_ac_ram_set_mode(lcd_ac_ram_t *ram, lcd_ac_mode_t mode) {
	switch(mode) {
		case LCD_MODE_COOL: ram->st.M1 = 1; ram->st.M2 = 0; ram->st.M3 = 0; break;
		case LCD_MODE_WARM: ram->st.M1 = 0; ram->st.M2 = 1; ram->st.M3 = 0; break;
		case LCD_MODE_WIND: ram->st.M1 = 0; ram->st.M2 = 0; ram->st.M3 = 1; break;
		default: break;
	}
}

void lcd_ac_set(const driver_ht1621_t* dev, lcd_ac_mode_t mode, lcd_ac_fan_speed_t speed, uint8_t temp_room, uint8_t temp_target) {
	lcd_ac_ram_t ram;
	lcd_ac_ram_clear(&ram);
	lcd_ac_ram_set_base(&ram, true);
	lcd_ac_ram_set_mode(&ram, mode);
	lcd_ac_ram_set_fan_speed(&ram, speed);
	lcd_ac_ram_set_temp_room(&ram, temp_room);
	lcd_ac_ram_set_temp_target(&ram, temp_target);
	lcd_ac_write(dev, &ram);
}

static void lcd_ac_test1(const driver_ht1621_t* dev) {
	for(int seg=0; seg <= 8; seg++) {
		for(int com=0; com <= 3; com++) {
			uint8_t val = 1 << com;
			ht1621_write(dev, seg, &val, 1);
		}
	}
}

#define tdelay() mcu_delay_ms(150)

static void lcd_ac_test(const driver_ht1621_t* dev) {
	lcd_ac_ram_t ram;

	lcd_ac_ram_clear(&ram);
	lcd_ac_ram_set_base(&ram, true); 
	lcd_ac_write(dev, &ram);
	tdelay();
	
	lcd_ac_ram_clear(&ram);
	lcd_ac_ram_set_mode(&ram, LCD_MODE_COOL);
	lcd_ac_write(dev, &ram);
	tdelay();

	lcd_ac_ram_clear(&ram);
	lcd_ac_ram_set_mode(&ram, LCD_MODE_WARM);
	lcd_ac_write(dev, &ram);
	tdelay();

	lcd_ac_ram_clear(&ram);
	lcd_ac_ram_set_mode(&ram, LCD_MODE_WIND);
	lcd_ac_write(dev, &ram);
	tdelay();

	lcd_ac_ram_clear(&ram);
	lcd_ac_ram_set_fan_speed(&ram, LCD_FAN_SPEED_LOW);
	lcd_ac_write(dev, &ram);
	tdelay();

	lcd_ac_ram_clear(&ram);
	lcd_ac_ram_set_fan_speed(&ram, LCD_FAN_SPEED_MEDIUM);
	lcd_ac_write(dev, &ram);
	tdelay();

	lcd_ac_ram_clear(&ram);
	lcd_ac_ram_set_fan_speed(&ram, LCD_FAN_SPEED_HIGH);
	lcd_ac_write(dev, &ram);
	tdelay();
	
	for(int i = 0; i <= LCD_8SEG_CHAR_MAX; i++) {
		lcd_ac_ram_clear(&ram);
		lcd_ac_ram_set_char1(&ram, (lcd_ac_8seg_char_t)i);
		lcd_ac_write(dev, &ram);
		tdelay();
	}
	
	for(int i = 0; i <= LCD_8SEG_CHAR_MAX; i++) {
		lcd_ac_ram_clear(&ram);
		lcd_ac_ram_set_char2(&ram, (lcd_ac_8seg_char_t)i);
		lcd_ac_write(dev, &ram);
		tdelay();
	}
	
	for(int i = 0; i <= LCD_8SEG_CHAR_MAX; i++) {
		lcd_ac_ram_clear(&ram);
		lcd_ac_ram_set_char3(&ram, (lcd_ac_8seg_char_t)i);
		lcd_ac_write(dev, &ram);
		tdelay();
	}
	
	for(int i = 0; i <= LCD_8SEG_CHAR_MAX; i++) {
		lcd_ac_ram_clear(&ram);
		lcd_ac_ram_set_char4(&ram, (lcd_ac_8seg_char_t)i);
		lcd_ac_write(dev, &ram);
		tdelay();
	}
	
	for(int i = 20; i < 35; i++) {
		lcd_ac_ram_clear(&ram);
		lcd_ac_ram_set_temp_room(&ram, i);
		lcd_ac_write(dev, &ram);
		tdelay();
	}

	for(int i = 20; i < 35; i++) {
		lcd_ac_ram_clear(&ram);
		lcd_ac_ram_set_temp_target(&ram, i);
		lcd_ac_write(dev, &ram);
		tdelay();
	}
	
	lcd_ac_set(dev, LCD_MODE_COOL, LCD_FAN_SPEED_MEDIUM, 32, 25);
}
